﻿namespace Jermaine.Dynamic
{
    using System;
    using System.Collections.Generic;
    using System.Dynamic;

    public class DynamicProxy
    {
        private DynamicProxy() { }

        public static dynamic Extend(ExpandoObject source, ExpandoObject options, bool overwrite = false) {
            var sourceSet = source as IDictionary<string, Object>;
            foreach (var item in options) {
                if (!sourceSet.ContainsKey(item.Key)) {
                    sourceSet.Add(item.Key, item.Value);
                } else {
                    if (!overwrite) {
                        sourceSet[item.Key] = item.Value;
                    }
                }
            }

            return source;
        }

        public static dynamic Extend(ExpandoObject source, IDictionary<string, Object> options, bool overwrite = false) {
            var sourceSet = source as IDictionary<string, Object>;

            foreach (var item in options) {
                if (!sourceSet.ContainsKey(item.Key)) {
                    sourceSet.Add(item.Key, item.Value);
                } else {
                    if (!overwrite) {
                        sourceSet[item.Key] = item.Value;
                    }
                }
            }
            return source;
        }

        public static dynamic Extend(IDictionary<string, Object> options) {
            var expandoObject = new ExpandoObject();
            var expandoSet = expandoObject as IDictionary<string, Object>;
            foreach (var item in options) {
                expandoSet.Add(item.Key, item.Value);
            }
            return expandoObject;
        }


    }
}
